<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\{
    Admin as AdminModel
};
class Admin extends Common{
    //管理员模型
    protected $admin;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->admin = new AdminModel();
    }
    /**
     *管理员列表
     * return json
     */
    public function adminList(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
       $list = $this->admin->select();
       if(empty($list)){
           $msg['message']=checkMsg(GET_DATA_FAILE);
       }else{
           $msg['message']=checkMsg(GET_DATA_SUCCESS);
           $msg['list']   = $list;
       }
       return $msg;
    }
    /**
     *管理员信息
     * return json
     */
    public function adminInfo(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $admin_id = $this->request->post('admin_id');
        if(empty($admin_id)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $info = $this->admin->where(['id'=>$admin_id])->find();
        if(empty($info)){
            $msg['message']=checkMsg(GET_DATA_FAILE);
        }else{
            $msg['message']=checkMsg(GET_DATA_SUCCESS);
            $msg['info'] =$info;
        }
        return $msg;
    }
    /**
     *添加或修改管理员信息
     *return json
     */
    public function addOrEditeAdmin(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $admin_id = $this->request->post('admin_id');
        $admin_name = $this->request->post('admin_name');
        $password   = $this->request->post('password');
        $user_type       = $this->request->post('user_type',0);  //1:超级管理员 0:普通管理员
        if(empty($admin_name) || empty($password)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        if($user_type==1){
            $count = $this->admin->where(['user_type'=>1])->count();
            if($count>0){
                $msg['message']=checkMsg(SUPER_ADMIN_EXIST);
                return $msg;
            }
        }
        $count = $this->admin->where(['user_login'=>$admin_name])->count();
        if($count>0){
            $msg['message']=checkMsg(ADMIN_IS_EXITS);
            return $msg;
        }

        if(!empty($admin_id)){  //修改
            $data=[
                'user_login'  =>$admin_name,
                'user_pass'    =>MD5($password),
                'user_type'   =>$user_type
            ];
            $res = $this->admin->save($data,['id'=>$admin_id]);
        }else{ //添加
            $data=[
                'user_login'  =>$admin_name,
                'user_pass'   =>MD5($password),
                'user_type'   =>$user_type,
                'create_time'  =>time()
            ];

            $res = $this->admin->insertGetId($data);
        }
        if($res){
            $msg['message']=checkMsg(OPERA_SUCCESS);
        }else{
            $msg['message']=checkMsg(OPERA_FAIL);
        }
        return $msg;
    }
    /**
     *删除或拉黑管理员
     * return json
     */
    public function deleteOrShield(){
        if(empty($this->adminInfo)){
            $msg['message']=checkMsg(LOGIN_ERROR);
            return $msg;
        }
        $admin_id = $this->request->post('admin_id');
        $type = $this->request->post('type',1); //1:删除 2:拉黑
        if(empty($admin_id)){
            $msg['message']=checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $adminInfo = $this->admin->where(['id'=>$admin_id])->find();
        if(empty($adminInfo)){
            $msg['message']=checkMsg(ADMIN_IS_NOT_EXIST);
            return $msg;
        }else{
            if($adminInfo['user_type']==1){
                $msg['message']=checkMsg(IS_SUPER_ADMIN);
                return $msg;
            }
            if($type==1){  //删除
               $res = $this->admin->where(['id'=>$admin_id])->delete();
            }else{  //拉黑
                $res = $this->admin->save(['user_status'=>0],['id'=>$admin_id]);
            }
            if($res){
                $msg['message']=checkMsg(OPERA_SUCCESS);
            }else{
                $msg['message']=checkMsg(OPERA_FAIL);
            }
            return $msg;
        }
    }
}